<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="../node_modules/jquery/dist/jquery.min.js"></script>
    <script src="../dist/pandyle.js"></script>
    <title>Document</title>
</head>

<body>
    <div class="main">
        <div p-com="test"></div>
        <div p-com="MyCom.book" p-context="book1"></div>
        <div p-com="Menu.menu"></div>
        <div p-com="list" p-context="@root|{title: '列表1', name: 'checkList', list:list}"></div>
        <button onclick="showData()">click me</button>
        <div p-for="components" p-as="item">
            <div p-bind="p-com:{{@item.type}}" p-context="@item.data"></div>
        </div>
    </div>

    <script>
        Pandyle.config({
            comPath: {
                Default: './otherComponents/{name}.html',
                Menu: './otherComponents/menu/{name}.html',
                MyCom: './components/{name}.html'
            }
        })

        var book1 = {
            title: 'book1',
            author: 'rrr',
            price: 20,
            num: 10,
            tags: ['tag1', 'tag2', 'tag3'],
            add: getAddFunc('book1'),
            buy: buyBook('book1')
        };

        var book2 = {
            title: 'book2',
            author: 'aaa',
            price: 30,
            num: 20,
            tags: ['tag1', 'tag2', 'tag3'],
            add: getAddFunc('book2'),
            buy: buyBook2(this)
        };

        var list = [
            {
                value: 1,
                name: '选项1'
            },
            {
                value: 2,
                name: '选项2'
            }
        ]
        var vm = $('.main').vm({
            book1: book1,
            book2: book2,
            list: list,
            components: [
                {
                    type: 'test'
                },
                {
                    type: 'MyCom.book',
                    data: book1
                },
                {
                    type: 'list',
                    data: {
                        title: '列表1',
                        name: 'checkList',
                        list: list
                    }
                }
            ]
        })

        function buyBook(name) {
            return function () {
                var book = vm.get(name);
                console.log(book);
            }

        }

        function buyBook2(obj) {
            return function () {
                console.log(obj);
            }
        }

        function getAddFunc(name) {
            return function () {
                var num = vm.get(name).num + 1;
                vm.set(name + '.num', num);
            }
        }

        function showData() {
            console.log($('.main').inputs().data());
        }
    </script>
</body>

</html>